import Vue from 'vue'
import Vuex from 'vuex'
import {
	loginOut
} from "@/api/login.js"

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: uni.getStorageSync('userInfo') ? true : false,
		userInfo: uni.getStorageSync('userInfo') || {},
		access_token: uni.getStorageSync('access_token') || '',
		isLoading: false,
	},
	mutations: {
		storeLogin(state, data) {
			// console.log('store', state, data);
			uni.setStorageSync('access_token', data.token);
			uni.setStorageSync('userInfo', data);
			uni.setStorageSync("notifyShowName", data.description)
			state.userInfo = data
			state.access_token = data.token
			state.hasLogin = true;
		},
		storeLogout(state) {
			loginOut(state.userInfo.id).then(res => {
				if (res.code == 200) {
					uni.removeStorageSync("access_token");
					uni.removeStorageSync("userInfo");
					state.hasLogin = false;
					state.userInfo = {}
					state.access_toke = ''
					uni.reLaunch({
						url: '/pages/login/login'
					})
				}
			})
		},
		storeLoading(state, data) {
			state.isLoading = data

		}
	}
})

export default store